package com.mopa.qzone;
import com.mopa.shareAlll.ShareAllActivity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
public class QZoneLayout extends RelativeLayout{
    private static final int ID_BTN_QQLOGIN = 11;
	private static final int ID_BTN_UPLOAD_PIC = 12;
	private Button mBtnQQLogin;
    private Button mBtnUpLoadPic;
	public QZoneLayout(Context context) {
		super(context);
		initLayout(context);
		// TODO Auto-generated constructor stub
	}
	private void initLayout(Context context) {
		// TODO Auto-generated method stub
	   RelativeLayout.LayoutParams relativeParams=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
	   relativeParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
	   relativeParams.setMargins(10, 0, 10, 0);
	   RelativeLayout mainLayout=new RelativeLayout(context);
	   this.addView(mainLayout, relativeParams);
	   
	   relativeParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
	   relativeParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
	   relativeParams.topMargin=80;
	   mBtnQQLogin=new Button(context);
	   mBtnQQLogin.setText("QQ Login");
	   mBtnQQLogin.setId(ID_BTN_QQLOGIN);
	   mBtnQQLogin.setOnClickListener(mOnClickListener);
	   mainLayout.addView(mBtnQQLogin, relativeParams);
	   
	   relativeParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
	   relativeParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
	   relativeParams.addRule(RelativeLayout.BELOW,ID_BTN_QQLOGIN);
	   relativeParams.topMargin=80;
	   mBtnUpLoadPic=new Button(context);
	   mBtnUpLoadPic.setText("Upload Pic");
	   mBtnUpLoadPic.setId(ID_BTN_UPLOAD_PIC);
	   ShareAllActivity activity = (ShareAllActivity)context;
	   mBtnUpLoadPic.setOnClickListener(new UploadPicClickListener(activity));
	   mainLayout.addView(mBtnUpLoadPic, relativeParams);
	   
	}
	private OnClickListener mOnClickListener=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			int vId=v.getId();
			switch (vId) {
			case ID_BTN_QQLOGIN:
				onQSAuth(QZoneItem.APPID,QZoneItem.SELF);
				break;
			case ID_BTN_UPLOAD_PIC:
				break;
			default:
				break;
			}
		}
	};
	protected void onQSAuth(String appid, String target) {
		// TODO Auto-generated method stub
		Intent intent = new Intent(getContext(), WebViewActivity.class);
		intent.setAction(WebViewActivity.AUTH_BROADCAST);
		String mLoadURL = "https://graph.qq.com/oauth2.0/authorize?response_type=token&display=mobile&client_id=%s&scope=%s&redirect_uri=%s";
		String url = String.format(mLoadURL, new Object[] {
				QZoneItem.APPID, QZoneItem.SCOPE, QZoneItem.CALLBACK});
		intent.setData(Uri.parse(url));
		getContext().startActivity(intent);
	}

}
